{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "82e43edb",
   "metadata": {},
   "source": [
    "# 学习内容：元组 集合 字典\n",
    "# 记录人：张钰华\n",
    "# 时间：2022-10-25"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ea33716",
   "metadata": {},
   "source": [
    "# 一.回顾上周知识\n",
    "* 1.while True---无限循环，可以实现for循环的指定次数，增加一个count计数器即可。\n",
    "* 2.while True---一般与用户输入一起使用，让用户可以重复输入，知道某些条件成立结束输入。（购物车）---平时作业"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "60313273",
   "metadata": {},
   "source": [
    "# 二.本周学习内容（元组/集合/字典）\n",
    "* 自学python（web---python.org）：tuples and sequences（序列：list/tuple/range）\n",
    "* index and slicing（索引值和切片）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "94ac918e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(210017, 210017, 210019) <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "# 1.元组（tuple）的基本内容（定义，存在价值）\n",
    "# 2.基本形态---以英文符号的原括号表示()\n",
    "网二_学号 = (210017,210017,210019)\n",
    "print(网二_学号,type(网二_学号))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "5074a3c8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(220017, 220017, 220019) <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "网三_学号 = 220017,220017,220019  #没有圆括号时也可以是元组的表现\n",
    "print(网三_学号,type(网三_学号))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f6acb6d1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "220017"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3.元组的性质\n",
    "# 3.1有序性（indexing；slicing）\n",
    "# 3.2不可变性（不可增/删/改）,实质是元组存放的空间没变\n",
    "网三_学号[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3dd707a5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(220017, 220019)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_学号[1:] #切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "0bd2a401",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2022, tm_mon=10, tm_mday=25, tm_hour=15, tm_min=6, tm_sec=33, tm_wday=1, tm_yday=298, tm_isdst=0)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import time\n",
    "time.localtime()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "308563d1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "([1, 2, 3, 4], [3, 4, 5, 6]) <class 'tuple'>\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(None, 2510814111936)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tuple_test = ([1,2,3,4],[3,4,5,6])\n",
    "print(tuple_test,type(tuple_test)),id(tuple_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "fcf2572e",
   "metadata": {},
   "outputs": [],
   "source": [
    "tuple_test[0].append(100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "68ddf3af",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([1, 2, 3, 4, 100], [3, 4, 5, 6])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tuple_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "c6d42444",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "([1, 2, 3, 4], [3, 4, 5, 6]) <class 'tuple'> 2510814111936\n",
      "[1, 2, 3, 4] 2510814067136\n"
     ]
    }
   ],
   "source": [
    "print(tuple_test,type(tuple_test),id(tuple_test))\n",
    "print(tuple_test[0],id(tuple_test[0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "d1af9579",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['apple', 'orange', 'apple', 'pear', 'orange', 'banana']\n",
      "{'banana', 'apple', 'orange', 'pear'}\n"
     ]
    }
   ],
   "source": [
    "# 集合\n",
    "# 1.输出方法{}\n",
    "# 2.性质 ：没有重复项（一一对应的关系）；无序性；\n",
    "# 3.方法：去掉重复项\n",
    "basket_lists = [\"apple\",\"orange\",\"apple\",\"pear\",\"orange\",\"banana\"]\n",
    "basket_sets = {\"apple\",\"orange\",\"apple\",\"pear\",\"orange\",\"banana\"}\n",
    "print(basket_lists)\n",
    "print(basket_sets)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "90b331f9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 统计社团人数\n",
    "# 方法一\n",
    "舞蹈社 ={1801,1802,1818,1830,1888,1821,1830}\n",
    "电竞社 ={1830,1877,1890,1802,1833,1865,1821}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd7582b6",
   "metadata": {},
   "source": [
    "* 集合的运算与交集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "7cad60d7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1802, 1821, 1830}"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "舞蹈社 & 电竞社  # 交集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "181aa0bf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1802, 1821, 1830}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "舞蹈社.intersection(电竞社) # 交集"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "27774ba2",
   "metadata": {},
   "source": [
    "* 集合的运算与并集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "c4d84f67",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1801, 1802, 1818, 1821, 1830, 1833, 1865, 1877, 1888, 1890}"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "舞蹈社|电竞社"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "38b7f610",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(舞蹈社|电竞社)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "7d20f089",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1801, 1802, 1818, 1821, 1830, 1833, 1865, 1877, 1888, 1890}"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "舞蹈社.union(电竞社)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e53ddd53",
   "metadata": {},
   "source": [
    "# 字典（dict）---{键，值，对 }\n",
    "* 一.字典概念\n",
    "* 二.无序性\n",
    "* 2.1keys是不可变的 ,可以把他删掉，但只要创建了就不会改变\n",
    "* 2.1keys是唯一的\n",
    "* 三.字典的形态\n",
    "* 四.字典的创建\n",
    "* 五.字典的函数及方法（增删改）\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "9b589d49",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{21101130058: 'david', 21101130059: 'mary'} <class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "网新四班 = {21101130058 :\"david\",\n",
    "       21101130059:\"mary\"}\n",
    "print(网新四班,type(网新四班))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "08f83598",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'david'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 字典的取值：通过keys进行字典的取值，unlike列表，元组，和字符串通过index取值\n",
    "网新四班[21101130058]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "a6cacc1f",
   "metadata": {},
   "outputs": [],
   "source": [
    "网新四班 = {21101130058 :[\"david\",\"广州\",\"男\",\"聊天记录\",\"朋友圈\"],\n",
    "       21101130059:\"mary\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "4c721382",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['david', '广州', '男', '聊天记录', '朋友圈']"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网新四班[21101130058]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "e86d1feb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字典的方法\n",
    "# 1.按照字典的形式直接创建（key:value）\n",
    "# 2.使用dict（）方法创建字典\n",
    "网新6班 = dict(\n",
    "    stu211030077=\"mary\",\n",
    "stu211030079=\"terry\",\n",
    "stu211030087=\"max\",\n",
    "stu211030097=\"halo\",)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "fae04b12",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu211030077': 'mary',\n",
       " 'stu211030079': 'terry',\n",
       " 'stu211030087': 'max',\n",
       " 'stu211030097': 'halo'}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网新6班"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "12758b9a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 增加 ：dict[key] = value\n",
    "网新6班 = {}\n",
    "网新6班[\"stu211030077\"] = \"mary\"\n",
    "网新6班[\"stu211030079\"] = \"terry\"\n",
    "网新6班[\"stu211030087\"] = \"max\"\n",
    "网新6班[\"stu211030097\"] = \"halo\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "5987621c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu211030077': 'mary',\n",
       " 'stu211030079': 'terry',\n",
       " 'stu211030087': 'max',\n",
       " 'stu211030097': 'halo'}"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网新6班"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "044152d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "网新6班_keys = [\"stu211030077\",\"stu211030079\",\"stu211030087\",\"stu211030097\"]\n",
    "网新6班_values = [\"mary\",\"terry\",\"max\",\"halo\"]\n",
    "网新6班 = {}\n",
    "for i in range(4):\n",
    "    网新6班[网新6班_keys[i]] = 网新6班_values[i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "3612074b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu211030077': 'mary',\n",
       " 'stu211030079': 'terry',\n",
       " 'stu211030087': 'max',\n",
       " 'stu211030097': 'halo'}"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网新6班"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7360cb76",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
